package com.springclouddemo.rabbitmq.demo1;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.springclouddemo.rabbitmq.config.RabbitConfig;
import com.springclouddemo.rabbitmq.config.TestDto;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Component
public class RabbitConsumer {
    @RabbitListener(queues = {RabbitConfig.DEMO_QUEUE})
    @RabbitHandler
    public void recieved(String msg) {
        System.out.println("[demoQueue] recieved message: " + msg);
    }

    @RabbitListener(queues = {RabbitConfig.OBJECT_QUEUE})
    @RabbitHandler
    public void receiveObject(TestDto dto) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String message = objectMapper.writeValueAsString(dto);
        System.out.println("simple consumer receive the object:"+ message);
    }
}
